import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.ImageIcon;

public class Game extends Core implements KeyListener {
	private Player leftPlayer, rightPlayer;
	private HashMap<String, AudioClip> soundsMap;
	private ArrayList<AudioClip> playingClips;
	
	public static void main(String[] args) {
		new Game().run();
	}

	// initializes game components
	public void init() {
		super.init();
		Window w = s.getFullScreenWindow();
		w.setFocusTraversalKeysEnabled(false);
		w.addKeyListener(this);
		rightPlayer = new Player("right", rightPlayer, (s.getWidth()/4*3) - 124, s.getHeight() - 348);
		leftPlayer = new Player("left", leftPlayer, s.getWidth()/4, s.getHeight() - 348);
		playingClips = new ArrayList<AudioClip>();
		loadSounds();
		playMusic(true);
	}
	
	//loads the music
	private void loadSounds(){
		soundsMap = new HashMap<String, AudioClip>();
		AudioClip clip = Applet.newAudioClip(getClass().getResource("sounds/background.wav"));
		if (clip == null){
			System.out.println("Problem loading sound");
		}else{
			soundsMap.put("background.wav", clip);
		}
	}
	
	//stops the music
	private void stopMusic(){
		if(!playingClips.isEmpty()){
			AudioClip audioClip;
			for(int i=0; i < playingClips.size(); i++){
				audioClip = (AudioClip) playingClips.get(i);
				audioClip.stop();
			}
			playingClips.clear();
		}
	}
	//stops the game
	public void stop(){
		super.stop();
		stopMusic();
	}
	
	//plays the music
	private void playMusic(boolean toLoop){
		String chosenFile = "background.wav";
		AudioClip audioClip = (AudioClip) soundsMap.get(chosenFile);
		if (audioClip == null){
			System.out.println("could not play file");
			return;
		}
		
		if(toLoop)
			audioClip.loop();
		else
			audioClip.play();
		
		playingClips.add(audioClip);
	}

	// updates animation
	public void update(long timePassed) {
		if(leftPlayer.getX() <= 0 && leftPlayer.getVelocityX()<=0){ //if it is going off the screen on the left
			leftPlayer.setVelocityX(0f);			
		}
		if(leftPlayer.getX()+leftPlayer.getWidth() >= rightPlayer.getX() && 
				(leftPlayer.getVelocityX()>0 || rightPlayer.getVelocityX()<0)){
			leftPlayer.setVelocityX(0f);
			rightPlayer.setVelocityX(0f);
		}
		if(rightPlayer.getX()+rightPlayer.getWidth()>=s.getWidth() && rightPlayer.getVelocityX()>=0){
			rightPlayer.setVelocityX(0f);
		}
		leftPlayer.update(timePassed, s.getWidth());
		rightPlayer.update(timePassed, s.getWidth());
	}

	public synchronized void draw(Graphics2D g) {
		Window w = s.getFullScreenWindow();
		g.setColor(w.getBackground());
		g.fillRect(0, 0, s.getWidth(), s.getHeight());
		//draws background image
		Image back = new ImageIcon(currDirectory() + "images\\background.jpg").getImage();
		g.drawImage(back, 0,0,null);
		
		g.setColor(Color.BLACK);
		g.drawImage(leftPlayer.getImage(), Math.round(leftPlayer.getX()),
				Math.round(leftPlayer.getY()), null);
		g.drawString("P1 Health: " + leftPlayer.getHealth(), s.getWidth()/16, 20);
		g.drawImage(rightPlayer.getImage(), Math.round(rightPlayer.getX()),
				Math.round(rightPlayer.getY()), null);
		g.drawString("P2 Health: " + rightPlayer.getHealth(), (s.getWidth()/16*15) - 130, 20);
	}

	// ////KEY LISTENERS//////////////
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_ESCAPE) {
			stop();
		} else if (keyCode == KeyEvent.VK_A) {
			leftPlayer.setVelocityX(-0.3f);
		} else if (keyCode == KeyEvent.VK_D) {
			leftPlayer.setVelocityX(0.3f);
		} else if (keyCode == KeyEvent.VK_W) {
			leftPlayer.jump();
		}else if (keyCode == KeyEvent.VK_TAB) {
			leftPlayer.punch(rightPlayer);
		} else if (keyCode == KeyEvent.VK_CAPS_LOCK) {
			leftPlayer.kick(rightPlayer);
		} else if (keyCode == KeyEvent.VK_RIGHT) {
			rightPlayer.setVelocityX(0.3f);
		} else if (keyCode == KeyEvent.VK_LEFT) {
			rightPlayer.setVelocityX(-0.3f);
		} else if (keyCode == KeyEvent.VK_NUMPAD1) {
			rightPlayer.punch(leftPlayer);
		} else if (keyCode == KeyEvent.VK_NUMPAD2) {
			rightPlayer.kick(leftPlayer);
		} 
		e.consume();

	}

	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == KeyEvent.VK_A) {
			leftPlayer.setVelocityX(0f);
		} else if (keyCode == KeyEvent.VK_D) {
			leftPlayer.setVelocityX(0f);
		} else if (keyCode == KeyEvent.VK_RIGHT) {
			rightPlayer.setVelocityX(0f);
		} else if (keyCode == KeyEvent.VK_LEFT) {
			rightPlayer.setVelocityX(0f);
		}
		e.consume();
	}

	public void keyTyped(KeyEvent e) {
		e.consume();
	}
	// //////////////////////
}
